statefiles: add a dirfd helper function
authorDavid Härdeman <[email protected]>
Fri, 28 Nov 2025 10:35:49 +0000 (11:35 +0100)
committerÁlvaro Fernández Rojas <[email protected]>
Tue, 9 Dec 2025 15:29:08 +0000 (16:29 +0100)
The same logic was repeated three times in config.c, move it
to a single function in statefiles.c.

Signed-off-by: David Härdeman <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/333
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/config.c
src/statefiles.c
src/statefiles.h

index 0e588b6f95a4ae5f91b294ae3eb5af3e46bdf592..97af99aec19de6a2d45f3a272ad14e804bfe0d24 100644 (file)
@@ -23,6 +23,7 @@
 #include "router.h"
 #include "dhcpv6-pxe.h"
 #include "dhcpv4.h"
+#include "statefiles.h"
 
 static struct blob_buf b;
 
@@ -2388,35 +2389,12 @@ void odhcpd_reload(void)
                char *file = basename(config.dhcp_statefile);
 
                memmove(config.dhcp_statefile, file, strlen(file) + 1);
-               mkdir_p(dir, 0755);
-
-               close(config.dhcp_statedir_fd);
-               config.dhcp_statedir_fd = open(dir, O_PATH | O_DIRECTORY | O_CLOEXEC);
-               if (config.dhcp_statedir_fd < 0)
-                       error("Unable to open statedir: '%s': %m", dir);
-       }
-
-       if (config.dhcp_hostsdir) {
-               char *dir = strdupa(config.dhcp_hostsdir);
-
-               mkdir_p(dir, 0755);
-
-               close(config.dhcp_hostsdir_fd);
-               config.dhcp_hostsdir_fd = open(dir, O_PATH | O_DIRECTORY | O_CLOEXEC);
-               if (config.dhcp_hostsdir_fd < 0)
-                       error("Unable to open hostsdir '%s': %m", dir);
-       }
-
-       if (config.ra_piofolder) {
-               char *path = strdupa(config.ra_piofolder);
-
-               mkdir_p(path, 0755);
-
-               close(config.ra_piofolder_fd);
-               config.ra_piofolder_fd = open(path, O_PATH | O_DIRECTORY | O_CLOEXEC);
-               if (config.ra_piofolder_fd < 0)
-                       error("Unable to open piofolder '%s': %m", path);
+               statefiles_setup_dirfd(dir, &config.dhcp_statedir_fd);
+       } else {
+               statefiles_setup_dirfd(NULL, &config.dhcp_statedir_fd);
        }
+       statefiles_setup_dirfd(config.dhcp_hostsdir, &config.dhcp_hostsdir_fd);
+       statefiles_setup_dirfd(config.ra_piofolder, &config.ra_piofolder_fd);
 
        vlist_flush(&lease_cfgs);
 
index da31ca6de7696d90c2eb8f37432cee54e3d80505..21201cbbf55a0760a9ea17281365883b54cbcc49 100644 (file)
@@ -338,3 +338,23 @@ bool statefiles_write()
 
        return true;
 }
+
+void statefiles_setup_dirfd(const char *path, int *dirfd)
+{
+       if (!dirfd)
+               return;
+
+       if (*dirfd >= 0) {
+               close(*dirfd);
+               *dirfd = -1;
+       }
+
+       if (!path)
+               return;
+
+       mkdir_p(strdupa(path), 0755);
+
+       *dirfd = open(path, O_PATH | O_DIRECTORY | O_CLOEXEC);
+       if (*dirfd < 0)
+               error("Unable to open directory '%s': %m", path);
+}
index 80be7468dd9c30a75cbd0fee220ec3ed0ba4894f..de7139776573daad997351c42bab487fdc56f92a 100644 (file)
@@ -12,4 +12,6 @@
 
 bool statefiles_write(void);
 
+void statefiles_setup_dirfd(const char *path, int *dirfdp);
+
 #endif /* _STATEFILES_H_ */